$(function () {
    var tableData = [];//全部数据集合
    var searchTableData = [];//查询出来的数据集合
    var searchAllData = true;//是否查询全部数据
    var ruleTable = null;
    var value = "";
    tableData = [
        {
            id: '100',
            ruleNumber: 'RA007',
            ruleNames: '跨制度参保-养老，机关保，新农保',
            ruleSummarize: '',
            checkRuleType: '跨制度重复参保',
            objectType: '参保人',
            currentRemark: '有效',
            areaCoding: '',
            templateName: '跨制度参保-养老，机关保，新农保',
            createWay: '',
            threa: '',
            createAnaly: '',
            ruleWay: '',
            scene: '',
            params: '',
            operator: '张三',
            operateTime: '',
            details: ''
        }, {
            id: '101',
            ruleNumber: 'RA008',
            ruleNames: '工伤生育缴费基数不一致',
            ruleSummarize: '',
            checkRuleType: '参保信息不相同',
            objectType: '参保人',
            currentRemark: '无效',
            areaCoding: '',
            templateName: '工伤生育缴费基数不一致',
            createWay: '',
            threa: '',
            createAnaly: '',
            ruleWay: '',
            scene: '',
            params: '',
            operator: '李四',
            operateTime: '',
            details: ''
        }, {
            id: '102',
            ruleNumber: 'RA009',
            ruleNames: '医保未参保仍正常待遇',
            ruleSummarize: '',
            checkRuleType: '参保待遇状态异常',
            objectType: '参保人',
            currentRemark: '有效',
            areaCoding: '',
            templateName: '医保未参保仍正常待遇',
            createWay: '',
            threa: '',
            createAnaly: '',
            ruleWay: '',
            scene: '',
            params: '',
            operator: '',
            operateTime: '',
            details: ''
        }, {
            id: '103',
            ruleNumber: 'RA010',
            ruleNames: '养老未参保仍正常待遇',
            ruleSummarize: '',
            checkRuleType: '参保信息不完整',
            objectType: '参保人',
            currentRemark: '有效',
            areaCoding: '',
            templateName: '参保信息不完整',
            createWay: '',
            threa: '',
            createAnaly: '',
            ruleWay: '',
            scene: '',
            params: '',
            operator: '陈华',
            operateTime: '2018-06-23',
            details: ''
        }, {
            id: '104',
            ruleNumber: 'RA011',
            ruleNames: '职工养老，农村养老，重复领取待遇',
            ruleSummarize: '',
            checkRuleType: '重复领取待遇',
            objectType: '参保单位',
            currentRemark: '有效',
            areaCoding: '',
            templateName: '职工养老，农村养老，重复领取待遇',
            createWay: '',
            threa: '',
            createAnaly: '',
            ruleWay: '',
            scene: '',
            params: '',
            operator: '叶一凡',
            operateTime: '2018-06-23',
            details: ''
        }, {
            id: '105',
            ruleNumber: 'RA012',
            ruleNames: '职工养老重复领取待遇',
            ruleSummarize: '',
            checkRuleType: '重复领取待遇',
            objectType: '参保人',
            currentRemark: '有效',
            areaCoding: '',
            templateName: '职工养老重复领取待遇',
            createWay: '',
            threa: '',
            createAnaly: '',
            ruleWay: '',
            scene: '',
            params: '',
            operator: '陈华',
            operateTime: '2012-06-23',
            details: ''
        }, {
            id: '106',
            ruleNumber: 'RA013',
            ruleNames: '城镇职工基本养老保险参保信息不完整',
            ruleSummarize: '',
            checkRuleType: '参保信息不完整',
            objectType: '参保人',
            currentRemark: '无效',
            areaCoding: '',
            templateName: '城镇职工基本养老保险参保信息不完整',
            createWay: '',
            threa: '',
            createAnaly: '',
            ruleWay: '',
            scene: '',
            params: '',
            operator: '陈华',
            operateTime: '2018-01-23',
            details: ''
        }, {
            id: '107',
            ruleNumber: 'RA014',
            ruleNames: '医保死亡，养老缴费比对',
            ruleSummarize: '',
            checkRuleType: '死亡状态不一致',
            objectType: '参保单位',
            currentRemark: '有效',
            areaCoding: '',
            templateName: '医保死亡，养老缴费比对',
            createWay: '',
            threa: '',
            createAnaly: '',
            ruleWay: '',
            scene: '',
            params: '',
            operator: '陈华',
            operateTime: '2007-06-23',
            details: ''
        }, {
            id: '108',
            ruleNumber: 'RA015',
            ruleNames: '养老死亡，医保参保缴费比对',
            ruleSummarize: '',
            checkRuleType: '死亡状态不一致',
            objectType: '参保人',
            currentRemark: '有效',
            areaCoding: '',
            templateName: '养老死亡，医保参保缴费比对',
            createWay: '',
            threa: '',
            createAnaly: '',
            ruleWay: '',
            scene: '',
            params: '',
            operator: '陈华',
            operateTime: '2018-06-23',
            details: ''
        },  {
            id: '110',
            ruleNumber: 'RA017',
            ruleNames: '城镇职工基本养老保险参保信息不完整',
            ruleSummarize: '',
            checkRuleType: '参保信息不完整',
            objectType: '参保人',
            currentRemark: '无效',
            areaCoding: '',
            templateName: '城镇职工基本养老保险参保信息不完整',
            createWay: '',
            threa: '',
            createAnaly: '',
            ruleWay: '',
            scene: '',
            params: '',
            operator: '陈华',
            operateTime: '2018-06-23',
            details: ''
        }, {
            id: '111',
            ruleNumber: 'RA018',
            ruleNames: '职工养老，农村养老，重复领取待遇',
            ruleSummarize: '',
            checkRuleType: '重复领取待遇',
            objectType: '参保单位',
            currentRemark: '有效',
            areaCoding: '',
            templateName: '职工养老，农村养老，重复领取待遇',
            createWay: '',
            threa: '',
            createAnaly: '',
            ruleWay: '',
            scene: '',
            params: '',
            operator: '王五',
            operateTime: '2018-01-24',
            details: ''
        }, {
            id: '112',
            ruleNumber: 'RA019',
            ruleNames: '医保死亡，养老缴费正常',
            ruleSummarize: '',
            checkRuleType: '死亡状态不一致',
            objectType: '参保单位',
            currentRemark: '有效',
            areaCoding: '',
            templateName: '医保死亡，养老缴费正常',
            createWay: '',
            threa: '',
            createAnaly: '',
            ruleWay: '',
            scene: '',
            params: '',
            operator: '陈华',
            operateTime: '2018-06-23',
            details: ''
        }, {
            id: '113',
            ruleNumber: 'RA020',
            ruleNames: '工伤生育缴费基数不一致',
            ruleSummarize: '',
            checkRuleType: '参保信息不相同',
            objectType: '参保人',
            currentRemark: '无效',
            areaCoding: '',
            templateName: '工伤生育缴费基数不一致',
            createWay: '',
            threa: '',
            createAnaly: '',
            ruleWay: '',
            scene: '',
            params: '',
            operator: '陈华',
            operateTime: '2018-06-23',
            details: ''
        }
    ];
    //layui使用--单文件使用
    layui.use(['form', 'layer', 'table', 'jquery', 'laydate'], function () {
        var $ = layui.jquery,
            form = layui.form,
            layer = layui.layer,
            table = layui.table,
            laydate = layui.laydate;
        //经办时间初始化
        laydate.render({
            elem: '#operateTime',
            format: "yyyy-MM-dd"
        });
        laydate.render({
            elem: '#operateTime2',
            format: "yyyy-MM-dd"
        });
        var layerLoad = null;
        //监听提交
        form.on('submit(demo1)', function (data) {
            searchTableData = [];
            var submitData = data.field;
            layerLoad = layer.load();
            //判断是否全部查询
            if (submitData.ruleNames || submitData.checkRuleTypes || submitData.auditType || submitData.objectType) {
                searchAllData = false;
            } else {
                searchAllData = true;
            }

            $.each(tableData, function (index, item) {
                if (submitData.checkRuleTypes && submitData.checkRuleTypes == item.checkRuleType) {
                    searchTableData.push(item);
                }
            });

            //销毁表格
            if (ruleTable != null) {
                ruleTable.clear();
                ruleTable.destroy();
            }
            initRuleTable();
            //只为了演示加载效果
            setTimeout(function () {
                layer.close(layerLoad);
            }, 500);
            return false;
        });

        var checkPopLayer = null;
        var checkPopLayer2 = null;
        var layer_area_coding = $("[name='layer_area_coding']");//统筹区编码
        var layer_rule_names = $("[name='layer_rule_names']");//规则名
        var layer_template_name = $("[name='layer_template_name']");//模板名称
        var layer_check_rule_types = $("[name='layer_check_rule_types']"); //比对规则类型
        var layer_rule_number = $("[name='layer_rule_number']"); //规则编号
        var layer_incre_ways = $("[name='layer_incre_ways']"); //增量实现方式
        var layer_thre_ways = $("[name='layer_thre_ways']"); //阈值比较方式
        var layer_incre_analysis = $("[name='layer_incre_analysis']"); //增量分析方式
        var layer_rule_ways = $("[name='layer_rule_ways']"); //规则实现方式
        var layer_scene_size = $("[name='layer_scene_size']"); //场景粒度
        var layer_scheme_params = $("[name='layer_scheme_params']"); //实现方案参数
        var layer_object_type = $("[name='layer_object_type']"); //对象类型
        var layer_handler = $("[name='layer_handler']"); //经办人
        var layer_handle_time = $("[name='layer_handle_time']"); //经办日期
        var layer_rule_description = $("[name='layer_rule_description']"); //规则描述

        //打开弹窗
        $(".operation").on("click", function () {
            //清空上一次操作的数值
            layer_area_coding.next().find("input").val("");
            layer_rule_names.val("");
            layer_template_name.val("");
            layer_check_rule_types.next().find("input").val("");
            layer_rule_number.val("");
            layer_incre_ways.val();
            layer_thre_ways.next().find("input").val("");
            layer_incre_analysis.val("");
            layer_rule_ways.val("");
            layer_scene_size.next().find("input").val("");
            layer_scheme_params.val("");
            layer_object_type.next().find("input").val("");
            layer_handler.val();
            layer_handle_time.val();
            layer_rule_description.val("");
            //操作判断
            var name = $(this).attr("data-item");
            if (name == 'add') {
                $("#save").show();
                layerSubmit(name);
                checkPopLayer = layer.open({
                    type: 1,
                    content: $("#layerMessage"),
                    area: ['1100px', '600px'],
                    title: '查看',
                    maxmin:true,
                    cancel: function () {

                    }
                });
            } else if (name == 'edit') {
                $("#save2").show();
                //判断是否有一个单选框被选定了
                /*var radioList = $("[name='indexer']");
                 var flag = false;
                 for(var i=0;i<radioList.length;i++){
                 console.log(radioList[i]);
                 }
                 if(flag){
                 layerSubmit(name);
                 }else{
                 layer.alert("请选择一个要编辑的规则");
                 return;
                 }*/
                $("[name='layer_rule_names2']").val("跨制度参保-养老，机关保，新农保");//规则名
                $("[name='layer_rule_number2']").val("TP021"); //规则编号
                $("[name='layer_handler2']").val("赖云浩"); //经办人
                $("[name='layer_handle_time2']").val("2013-01-06"); //经办日期
                $("[name='layer_rule_description2']").val("2018年新增规则"); //规则描述
                $("[name='layer_check_rule_types2']").next().find("input").val("跨制度参保");
                $("[name='layer_object_type2']").next().find("input").val("参保人");//对象类型
				layer_area_coding.next().find("input").val("天河区东莞");
				$("[name='layer_scheme_params2']").val("PKG_RA_CANBAOXXBUTONG.PRC_CANBAOXXBUTONG"); //规则参数
					
                checkPopLayer2 = layer.open({
                    type: 1,
                    content: $("#layerMessageEdit"),
                    area: ['1100px', '600px'],
                    title: '编辑',
                    maxmin:true,
                    cancel: function () {

                    }
                });
            } else if (name == '') {
                $("#save2").hide();
                $("[name='layer_rule_names2']").val("跨制度参保-养老，机关保，新农保");//规则名
                $("[name='layer_rule_number2']").val("TP021"); //规则编号
                $("[name='layer_handler2']").val("赖云浩"); //经办人
                $("[name='layer_handle_time2']").val("2013-01-06"); //经办日期
                $("[name='layer_rule_description2']").val("2018年新增规则"); //规则描述
                $("[name='layer_check_rule_types2']").next().find("input").val("跨制度参保");
                $("[name='layer_object_type2']").next().find("input").val("参保人");//对象类型
				layer_area_coding.next().find("input").val("天河区东莞");
				$("[name='layer_scheme_params2']").val("PKG_RA_CANBAOXXBUTONG.PRC_CANBAOXXBUTONG"); //规则参数
                checkPopLayer = layer.open({
                    type: 1,
                    content: $("#layerMessageEdit"),
                    area: ['1100px', '600px'],
                    title: '查看',
                    maxmin:true,
                    cancel: function () {

                    }
                });
            } else {
                layer.confirm('是否禁用此规则?', {icon: 3, title: '提示'}, function (index) {
                    //do something
                    $.each(tableData, function (i, item) {
                        if (item.id == value) {
                            item.currentRemark = '无效';
                        }
                    });

                    //销毁表格
                    if (ruleTable != null) {
                        ruleTable.clear();
                        ruleTable.destroy();
                    }
                    initRuleTable();
                    //单选按钮绑定事件
                    $(".selectRadio").click(function () {
                        value = $(this).attr("value");
                    });
                    layer.close(index);
                }, function (index) {

                });

            }
        });

        var index = 999;
        //监听编辑和新增提交
        function layerSubmit(str) {
            var areaValue = "";
            var ruleValue = "";
            var templateValue = "";
            var checkTypeValue = "";
            var numberValue = "";
            var createValue = "";
            var threadValue = "";
            var createAnalyVal = "";
            var ruleWayVal = "";
            var sceneVal = "";
            var paramVal = "";
            var objectVal = "";
            var operateVal = "";
            var operateTimeVal = "";
            var ruleDesVal = "";
            form.on("submit(demo2)", function (data) {
                var item = data.field;
                areaValue = item.layer_area_coding;
                ruleValue = item.layer_rule_names;
                templateValue = item.layer_template_name;
                checkTypeValue = item.layer_check_rule_types;
                numberValue = item.layer_rule_number;
                createValue = item.layer_incre_ways;
                threadValue = item.layer_thre_ways;
                createAnalyVal = item.layer_incre_analysis;
                ruleWayVal = item.layer_rule_ways;
                sceneVal = item.layer_scene_size;
                paramVal = item.layer_scheme_params;
                objectVal = item.layer_object_type;
                operateVal = item.layer_handler;
                operateTimeVal = item.layer_handle_time;
                ruleDesVal = item.layer_rule_description;

                //新增
                if (str == 'add') {
                    var addItem = {
                        id: index,
                        ruleNumber: numberValue,
                        ruleNames: ruleValue,
                        ruleSummarize: ruleDesVal,
                        checkRuleType: checkTypeValue,
                        objectType: objectVal,
                        currentRemark: '有效',
                        areaCoding: areaValue,
                        templateName: templateValue,
                        createWay: createValue,
                        threa: threadValue,
                        createAnaly: createAnalyVal,
                        ruleWay: ruleWayVal,
                        scene: sceneVal,
                        params: paramVal,
                        operator: operateVal,
                        operateTime: operateTimeVal,
                        details: ruleDesVal
                    };
                    tableData.unshift(addItem);
                    layer.alert("新增成功");
                    layer.close(checkPopLayer);
                    index++;
                    //销毁表格
                    if (ruleTable != null) {
                        ruleTable.clear();
                        ruleTable.destroy();
                    }
                    initRuleTable();

                }
                return false;
            });


        };
    });
    //初始化表格
    function initRuleTable() {

        ruleTable = $("#rulesTable").DataTable({
            searching: false,
            ordering: false,
            lengthChange: false,
            //retrieve:false,
            //国际化
            language: {
                search: "在表格中搜索:",
                "sInfo": "显示第 _START_ 至 _END_ 项结果，共 _TOTAL_ 项",
                "oPaginate": {
                    "sFirst": "首页",
                    "sPrevious": "上页",
                    "sNext": "下页",
                    "sLast": "末页"
                }
            },
            columns: [{
                data: 'id',
                render: function (data, type, row, meta) {
                    return '<input type="radio" name="indexer" class="selectRadio" value=' + data + '>';
                },
                width: '2%'
            }, {
                data: 'ruleNumber',
                width: '10%'
            }, {
                data: 'ruleNames',
                width: '18%'
            }, {
                data: 'ruleSummarize',
                width: '20%'
            }, {
                data: 'checkRuleType',
                width: '12%'
            }, {
                data: 'objectType',
                width: '10%'
            }, {
                data: 'currentRemark',
                width: '8%'
            }],
            data: searchAllData ? tableData : searchTableData
        });

        //ruleTable.draw();
    };




    function init() {
        initRuleTable();
        //单选按钮绑定事件

        $(".selectRadio").click(function () {
            value = $(this).attr("value");
        });
    };
    init();
});